$(document).ready(function () {


    /* LEFT SIDE DATEPICKER */
    $("#menuDatepicker").datepicker();
    /* UI elements datepicker */
    $("#Datepicker").datepicker();
    jQuery(".dataTable").find('[class^= icon-]').tooltip();


    /* CALENDAR */
    //    var date = new Date();
    //    var d = date.getDate();
    //    var m = date.getMonth();
    //    var y = date.getFullYear();

    //    var calendar = $('#calendar').fullCalendar({
    //            header: {		
    //                    left: 'prev,next',
    //                    center: 'title',
    //                    right: ''
    //                    //right: 'month,agendaWeek,agendaDay'
    //            },
    //            selectable: true,
    //            selectHelper: true,
    //            select: function(start, end, allDay) {
    //                    var title = prompt('Event Title:');
    //                    if (title) {
    //                            calendar.fullCalendar('renderEvent',
    //                                    {
    //                                            title: title,
    //                                            start: start,
    //                                            end: end,
    //                                            allDay: allDay
    //                                    },
    //                                    true // make the event "stick"
    //                            );
    //                    }
    //                    calendar.fullCalendar('unselect');
    //            },
    //            editable: true,
    //            events: [
    //                    {
    //                            title: 'All Day Event',
    //                            start: new Date(y, m, 20),
    //                            end: new Date(y, m, 21)
    //                    },
    //                    {
    //                            title: 'Long Event',
    //                            start: new Date(y, m, 1),
    //                            end: new Date(y, m, 10)
    //                    }
    //            ]
    //    });

    // SELECT2       
    $("#s2_1").select2();
    $(".s2_2").select2();

    // CHECKBOXES AND RADIO
    $(".row-form,.dialog,.loginBox,.block-fluid").find("input:checkbox, input:file").not(".skip").uniform();

    // MASKED INPUTS

    $("#mask_phone").mask('99 (999) 999-99-99');
    $("#mask_credit").mask('9999-9999-9999-9999');
    $("#mask_date").mask('99/99/9999');
    $("#mask_tin").mask('99-9999999');
    $("#mask_ssn").mask('999-99-9999');

    //FORM VALIDATION

    $("#validation").validationEngine({ promptPosition: "topLeft", scroll: true });

    // CUSTOM SCROLLING

    $(".scroll").mCustomScrollbar();

    // ACCORDION 

    $(".accordion").accordion();

    // PROGRESSBAR
    if ($("#progressbar-3").length > 0)
        $('#progressbar-3').progressbar({ value: 65 });

    if ($("#progressbar-1").length > 0)
        $("#progressbar-1").anim_progressbar();

    if ($("#progressbar-2").length > 0) {
        var iNow = new Date().setTime(new Date().getTime() + 3 * 1000);
        var iEnd = new Date().setTime(new Date().getTime() + 20 * 1000);
        $('#progressbar-2').anim_progressbar({ start: iNow, finish: iEnd, interval: 1 });
    }



    // DIALOG

    $("#b_popup_1").dialog({ autoOpen: false });

    $("#popup_1").click(function () { $("#b_popup_1").dialog('open') });

   

    $("#b_popup_3").dialog({ autoOpen: false, modal: true });

    $("#popup_3").click(function () { $("#b_popup_3").dialog('open') });  

    // SLIDER



    $("#slider_2").slider({
        values: [17, 67],
        orientation: "horizontal",
        range: true,
        animate: true,
        slide: function (event, ui) {
            $("#slider_2_amount").html("$" + ui.values[0] + " - $" + ui.values[1]);
        }
    });

    $("#slider_3").slider({
        orientation: "vertical",
        range: "min",
        min: 0,
        max: 100,
        value: 10,
        slide: function (event, ui) {
            $("#slider_3_amount").html('$' + ui.value);
        }
    });

    $("#slider_4").slider({
        orientation: "vertical",
        range: true,
        values: [17, 67]
    });

    $("#slider_5").slider({
        orientation: "vertical",
        range: "max",
        min: 1,
        max: 10,
        value: 2
    });


    // TABS

    $(".tabs").tabs();

    // TOOLTIPSborder:{

    $('.tt').qtip({ style: { name: 'aquarius' },
        position: {
            corner: {
                target: 'topRight',
                tooltip: 'bottomLeft'
            }
        }
    });

    $('.ttRC').qtip({ style: { name: 'aquarius' },
        position: {
            corner: {
                target: 'rightMiddle',
                tooltip: 'leftMiddle'
            }
        }
    });

    $('.ttRB').qtip({ style: { name: 'aquarius' },
        position: {
            corner: {
                target: 'bottomRight',
                tooltip: 'topLeft'
            }
        }
    });

    $('.ttLT').qtip({ style: { name: 'aquarius' },
        position: {
            corner: {
                target: 'topLeft',
                tooltip: 'bottomRight'
            }
        }
    });

    $('.ttLC').qtip({ style: { name: 'aquarius' },
        position: {
            corner: {
                target: 'leftMiddle',
                tooltip: 'rightMiddle'
            }
        }
    });

    $('.ttLB').qtip({ style: { name: 'aquarius' },
        position: {
            corner: {
                target: 'bottomLeft',
                tooltip: 'topRight'
            }
        }
    });


    // SORTABLE       
    $("#sort_1").sortable({ placeholder: "placeholder" });
    $("#sort_1").disableSelection();

    // SELECTABLE
    $("#selectable_1").selectable();


    // WYSIWIG HTML EDITOR
    if ($("#wysiwyg").length > 0) {
        editor = $("#wysiwyg").cleditor({ width: "100%", height: "100%" })[0].focus();
    }

    // Sortable table
    if ($("#tSortable").length > 0) {
        $("#tSortable").dataTable({ "iDisplayLength": 5, "aLengthMenu": [5, 10, 25, 50, 100], "sPaginationType": "full_numbers", "aoColumns": [{ "bSortable": false }, null, null, null, null] });
        $("#tSortable_2").dataTable({ "iDisplayLength": 5, "sPaginationType": "full_numbers", "bLengthChange": false, "bFilter": false, "bInfo": false, "bPaginate": true, "aoColumns": [{ "bSortable": false }, null, null, null, null] });
    }

});

$(window).resize(function() {

    if($("#wysiwyg").length > 0) editor.refresh();

    
});

function Ajax(_data) {

    var url = typeof _data.url !== "undefined" ? _data.url : location.pathname;

    var type = typeof _data.type !== "undefined" ? _data.type : "get";

    var data = typeof _data.data !== "undefined" ? _data.data : null;

    var dataType = typeof _data.dataType !== "undefined" ? _data.dataType : false;

    var mimeType = typeof _data.mimeType !== "undefined" ? _data.mimeType : null;

    var contentType = typeof _data.contentType !== "undefined" ? _data.contentType : "application/x-www-form-urlencoded; charset=UTF-8";

    var async = typeof _data.async !== "undefined" ? _data.async : true;

    var cache = typeof _data.cache !== "undefined" ? _ata.cache : false;

    var processData = typeof _data.processData !== "undefined" ? _data.processData : true;

    var isShowSuccessData = typeof _data.isShowSuccessData !== "undefined" && _data.isShowSuccessData ? true : false;

    var isShowErrorMessage = typeof _data.isShowErrorMessage !== "undefined" && _data.isShowErrorMessage ? true : false;

    jQuery.ajax({

        url: url,

        type: type,

        data: data,

        dataType: dataType,

        mimeType: mimeType,

        contentType: contentType,

        async: async,

        cache: cache,

        processData: processData,

        success: function (result, textStatus, jqXHR) {

            if (isShowSuccessData) {

                console.log(result);
            }

            if (typeof _data.ExecSuccess !== "undefined") {

                _data.ExecSuccess({ dataServer: result, dataClient: _data });
            }
        },

        error: function (jqXHR, textStatus, errorThrown) {

            if (isShowErrorMessage) {

                console.log("textStatus: " + textStatus + " | " + "errorThrown: " + errorThrown);
            }
        },

        beforeSend: function () { },

        complete: function () { }
    });
};

function DownloadFile(_data) {
    var frm_id = NewGuid({});
    var frm_name = frm_id;
    var frm_action = typeof _data.action !== "undefined" ? _data.action : "/";
    var frm_method = typeof _data.method !== "undefined" ? _data.method : "get";
    var frm_html = '<form id="' + frm_id + '" name="' + frm_name + '" action="' + frm_action + '" method="' + frm_method + '" enctype="multipart/form-data"></form>';
    var frm_data = _data.data;

    jQuery("body").append(frm_html);
    $frm = jQuery("form#" + frm_id);
    for (var attr in frm_data) {
        if (frm_data.hasOwnProperty(attr)) {
            var input_html = '<input type="hidden" name="' + attr + '" value="' + frm_data[attr] + '" />';
            $frm.append(input_html);
        }
    }
    $frm.submit();
    $frm.remove();
};

function NewGuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
};

function ParseTime(_data) {

    if (typeof _data !== "undefined") {        // Template: Date object        if (typeof _data == "object") {
            return _data;
        }        // Template: Number object        if (typeof _data == "number") {
            return new Date(_data);
        }        // Template: String object {        if (typeof _data == "string") {            // Template: "\/Date(INTEGER)\/"            if (/^\/Date\(\d+\)\/$/g.test(_data)) {
                var str = _data.replace(/\/Date\((-?\d+)\)\//, '$1');                return new Date(parseInt(str));
            }            // Template: "dd/MM/yyyy"            if (/^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/]\d{4}$/.test(_data)) {
                var arr = _data.split("/");                return new Date(parseInt(arr[2]), parseInt(arr[1]) - 1, parseInt(arr[0]), 0, 0, 0, 0);
            }

            // Template: "MM/yyyy"
            if (/^(0?[1-9]|1[012])[\/]\d{4}$/.test(_data)) {
                var arr = _data.split("/");
                return new Date(parseInt(arr[1]), parseInt(arr[0]) - 1, 1, 0, 0, 0, 0);
            }
        }
    }    return null;
};

function IsNullOrEmpty(_data) {
    return _data == undefined || _data == null || _data == "";
};

function FixCSSFancybox210(_data) {
    $(".fancybox-wrap").css("top", "24px");
    $(".fancybox-wrap, .fancybox-inner").css("height", "auto");
    $(".fancybox-inner").css("max-height", $(window).height() - 78 + "px");
    $(".fancybox-inner").css("overflow-y", "auto");
    $(".fancybox-nav").css("display", "none");
};
